package jhta;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class RequestBookInput extends JInternalFrame {
	Connection conn;
	Vector<Integer> bigVector = null;
	Vector<Integer> smallVector = null;
	
	private JLabel lblNewLabel;
	private JPanel panel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_3;
	private JTextField booktitle;
	private JTextField bookpublisher;
	private JScrollPane scrollPane;
	private JTextArea note;
	private JLabel lblNewLabel_4;
	private JButton btnNewButton;
	private JLabel lblNewLabel_5;
	private JComboBox bigCompo;
	private JComboBox smallCombo;


	public RequestBookInput() {
		super("도서 요청", true, true, true, true);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameActivated(InternalFrameEvent arg0) {
				setGroup();
			}
		});
		setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 625, 362);
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getLblNewLabel(), BorderLayout.NORTH);
		getContentPane().add(getPanel(), BorderLayout.CENTER);
		
		conn = JavaDBConnection.getConn();
		String sql = null;
		
	
	}
	
	public void inputData(){
		String sql = "insert into request(serial, mid, booktitle, bookpublisher, note) "
				+ " values(request_seq.nextval, ?,?,?,?"
				+ ")";
		
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, smallVector.get(smallCombo.getSelectedIndex()));
			ps.setString(2, booktitle.getText());
			ps.setString(3, bookpublisher.getText());
			ps.setString(4, note.getText());
			
			int cnt = ps.executeUpdate();
			if(cnt >0){
				JOptionPane.showMessageDialog(null, "도서 요청 자료가 입력되었습니다.");
				booktitle.setText("");
				bookpublisher.setText("");
				note.setText("");
				
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void setGroup(){
	
		String sql = "select code_no, code_name from code " 
				+ " where (code_big <> 'A1' and code_small is null)" 
				+ " or (code_big = 'A3' and code_small is not null)"
				+ " order by code_big";

		
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			bigVector = new Vector();
			bigCompo.removeAllItems();

			while(rs.next()){
				bigVector.add(rs.getInt("code_no"));
				bigCompo.addItem(rs.getString("code_name"));
			}
			
			rs.close();
			ps.close();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public void setSmall(int temp){
		int small = bigVector.get(temp);
		smallCombo.removeAllItems();
		String sql = "select no, name from bookmember m inner join code c"
					+ " on m.code_no = c.code_no "
					+ " where c.code_no = ? ";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, small);
			
			smallVector = new Vector<>();
			
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				smallVector.add(rs.getInt("no"));
				smallCombo.addItem(rs.getString("name"));
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	
	
	
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("도서 요청");
			lblNewLabel.setPreferredSize(new Dimension(52, 50));
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 30));
		}
		return lblNewLabel;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(null);
			panel.add(getLblNewLabel_1());
			panel.add(getLblNewLabel_2());
			panel.add(getLblNewLabel_3());
			panel.add(getBooktitle());
			panel.add(getBookpublisher());
			panel.add(getScrollPane());
			panel.add(getLblNewLabel_4());
			panel.add(getBtnNewButton());
			panel.add(getLblNewLabel_5());
			panel.add(getBigCompo());
			panel.add(getSmallCombo());
		}
		return panel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("요청 도서명");
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_1.setBounds(12, 10, 80, 15);
		}
		return lblNewLabel_1;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("출판사");
			lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_2.setBounds(12, 35, 80, 15);
		}
		return lblNewLabel_2;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("도서 요청자");
			lblNewLabel_3.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_3.setBounds(12, 95, 80, 15);
		}
		return lblNewLabel_3;
	}
	private JTextField getBooktitle() {
		if (booktitle == null) {
			booktitle = new JTextField();
			booktitle.setPreferredSize(new Dimension(6, 30));
			booktitle.setBounds(104, 7, 493, 25);
			booktitle.setColumns(10);
		}
		return booktitle;
	}
	private JTextField getBookpublisher() {
		if (bookpublisher == null) {
			bookpublisher = new JTextField();
			bookpublisher.setPreferredSize(new Dimension(6, 30));
			bookpublisher.setBounds(104, 32, 217, 25);
			bookpublisher.setColumns(10);
		}
		return bookpublisher;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(104, 113, 493, 127);
			scrollPane.setViewportView(getNote());
		}
		return scrollPane;
	}
	private JTextArea getNote() {
		if (note == null) {
			note = new JTextArea();
		}
		return note;
	}
	private JLabel getLblNewLabel_4() {
		if (lblNewLabel_4 == null) {
			lblNewLabel_4 = new JLabel("기타 내용");
			lblNewLabel_4.setBounds(35, 130, 57, 15);
		}
		return lblNewLabel_4;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("요청하기");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					inputData();
				}
			});
			btnNewButton.setBounds(104, 250, 97, 23);
		}
		return btnNewButton;
	}
	private JLabel getLblNewLabel_5() {
		if (lblNewLabel_5 == null) {
			lblNewLabel_5 = new JLabel("소 속");
			lblNewLabel_5.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_5.setBounds(35, 60, 57, 15);
		}
		return lblNewLabel_5;
	}
	private JComboBox getBigCompo() {
		if (bigCompo == null) {
			bigCompo = new JComboBox();
			bigCompo.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(bigCompo.getSelectedIndex() > -1){
						setSmall(bigCompo.getSelectedIndex());
					}
				}
			});
			bigCompo.setBounds(104, 59, 411, 25);
		}
		return bigCompo;
	}
	private JComboBox getSmallCombo() {
		if (smallCombo == null) {
			smallCombo = new JComboBox();
			smallCombo.setBounds(103, 89, 139, 25);
		}
		return smallCombo;
	}
}
